package com.hiyee.customer.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiyee.api.controller.BaseController;
import com.hiyee.api.controller.customer.GoodsControllerApi;
import com.hiyee.pojo.auth.LoginCustomer;
import com.hiyee.pojo.bo.GoodsSearchBO;
import com.hiyee.response.ResponseJsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class GoodsController extends BaseController implements GoodsControllerApi {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseJsonResult getH5Tree() throws JsonProcessingException {
        // 调用商品微服务获取数据
        String url = "http://localhost:9103/rest/api/goods/category/getH5Tree";
        ResponseEntity<ResponseJsonResult> responseEntity = restTemplate.getForEntity(url, ResponseJsonResult.class);
        ResponseJsonResult bodyResult = responseEntity.getBody();
        System.out.println("[文件服务] 返回值：" + bodyResult.getData().toString());
        return this.success(bodyResult.getData());
    }

    @Override
    public ResponseJsonResult searchGoods(String sortBy, String sortType, Integer categoryId, String name, Integer page, Integer pageSize, LoginCustomer loginCustomer) {
        System.out.println(sortBy + sortType + categoryId + pageSize);
        // 调用商品微服务获取数据
        String url = "http://localhost:9103/rest/api/goods/search";
        GoodsSearchBO goodsSearchBO = new GoodsSearchBO();
        goodsSearchBO.setCategoryId(categoryId);
        goodsSearchBO.setPage(page);
        goodsSearchBO.setPageSize(pageSize);
        goodsSearchBO.setName(name);
        goodsSearchBO.setSortBy(sortBy);
        goodsSearchBO.setSortType(sortType);
        ResponseEntity<ResponseJsonResult> responseEntity = restTemplate.postForEntity(url, goodsSearchBO, ResponseJsonResult.class);
        ResponseJsonResult bodyResult = responseEntity.getBody();
        System.out.println("[文件服务] 返回值：" + bodyResult.getData().toString());
        return this.success(bodyResult.getData());
    }

    @Override
    public ResponseJsonResult getGoods(Long goodsId, LoginCustomer loginCustomer) {
        // 调用商品微服务获取数据
        String url = "http://localhost:9103/rest/api/goods/getInfo?id=" + goodsId;
        ResponseEntity<ResponseJsonResult> responseEntity = restTemplate.getForEntity(url, ResponseJsonResult.class);
        ResponseJsonResult bodyResult = responseEntity.getBody();
        System.out.println("[文件服务] 返回值：" + bodyResult.getData().toString());
        return this.success(bodyResult.getData());
    }

    @Override
    public ResponseJsonResult getServiceList(Long goodsId) {
        return null;
    }
}
